//
//  DownloadSelectView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/21.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "DownloadSelectView.h"

@interface DownloadSelectView()

@property (nonatomic, strong) UIView *backgraoundView;
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSMutableArray *buttonsArray;
@end



@implementation DownloadSelectView


- (instancetype)init{
    if (self = [super init]) {
    
        [self loadSubviews];
    }
    return  self;
}

- (void)loadSubviews{
    self.backgroundColor = [UIColor clearColor];
    self.window = [UIApplication sharedApplication].keyWindow;
    [self.window addSubview:self.backgraoundView];
    
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor clearColor];
    self.contentView = contentView;
    [self.window addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.window);
        make.height.mas_equalTo(200*HEIGHT);
    }];
    
    UIView *selItemView = [[UIView alloc]init];
    selItemView.backgroundColor = KLColor_C1;
    [contentView addSubview:selItemView];
    selItemView.layer.masksToBounds = YES;
    selItemView.layer.cornerRadius = 10*WIDTH;
    [contentView addSubview:selItemView];
    [selItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(contentView);
        make.bottom.equalTo(contentView).offset(-20*HEIGHT);
    }];
    
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = @"选择下载质量";
    tintLb.textColor = KLColor_B6;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [selItemView addSubview:tintLb];
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(selItemView);
        make.top.equalTo(selItemView).offset(15*HEIGHT);
    }];
    
    
    NSArray *titleArr = @[@"标清",@"高清",@"超清"];
    for (NSInteger i = 0; i < titleArr.count; i++) {
        UIButton *qualityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [qualityBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        if (i == 0) {
            qualityBtn.backgroundColor = KLColor(0, 0, 0, 0.1);
        }else{
            qualityBtn.backgroundColor = [UIColor whiteColor];
        }
        [qualityBtn setTitleColor:KLColor_B6 forState:UIControlStateNormal];
        qualityBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        qualityBtn.tag = 100 + i;
        qualityBtn.layer.masksToBounds = YES;
        qualityBtn.layer.cornerRadius = 15*HEIGHT;
        [selItemView addSubview:qualityBtn];
        [qualityBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [qualityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(selItemView).offset(76*HEIGHT);
            if (i == 0) {
                make.left.equalTo(selItemView).offset(38*WIDTH);
            }else{
                UIButton *tempButton = self.buttonsArray[i-1];
                make.left.equalTo(tempButton.mas_right).offset(30*WIDTH);
               
            }
            make.width.mas_equalTo(80*WIDTH);
            make.height.mas_equalTo(30*HEIGHT);
            
        }];
        
        [self.buttonsArray addObject:qualityBtn];
    }
    
    
    
    UIView *cancelView = [[UIView alloc]init];
    cancelView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:cancelView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancel)];
    [cancelView addGestureRecognizer:tap];
    [cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(contentView);
        make.height.mas_equalTo(44*HEIGHT);
    }];
    
    UILabel *cancelTint = [[UILabel alloc]init];
    cancelTint.text = @"取消";
    cancelTint.textColor = KLColor_B12;
    cancelTint.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [cancelView addSubview:cancelTint];
    [cancelTint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(cancelView);
    }];
}

- (void)tapCancel{
    [self cancel];
}
- (void)cancel{
    [self.backgraoundView removeFromSuperview];
    [self.contentView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)click:(UIButton *)sender{
    [self.buttonsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        obj.enabled = NO;
        obj.backgroundColor = [UIColor whiteColor];
    }];
//    sender.enabled = YES;
    sender.backgroundColor = KLColor(0, 0, 0, 0.1);
}


- (UIView *)backgraoundView{
    if (!_backgraoundView) {
        _backgraoundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, KLScreenH)];
        _backgraoundView.backgroundColor = KLColor(0, 0, 0, 0.3);
    }
    return _backgraoundView;
}


- (NSMutableArray *)buttonsArray{
    if (!_buttonsArray) {
        _buttonsArray = [NSMutableArray array];
    }
    return _buttonsArray;
}
@end
